//
//  DUCalendarView.swift
//  SimpleCal
//
//  Created by 杜晓祥 on 2021/10/20.
//

import Cocoa

class DUCalendarView: NSViewController, DUPageSwitchProtocol {

    @IBOutlet weak var calendarView: CalendarView!
    @IBOutlet weak var dayDetail: DUDayDetailView!
    @IBOutlet weak var daySwitch: DUSwitchView!
    
    private var index:Int = -1
    private var nowViewController:NSViewController?
    private var pageTypeProtocol:DUMonthDayProtocol!
    
    private var pageType:PageSwitchEnum!
    
    override func viewDidLoad() {
        addChild(MonthDayViewController())
        addChild(MonthDayViewController())
        addChild(SelectYearMonthViewController())
        addChild(SelectYearMonthViewController())
        calendarView.addSubview(children[0].view)
        GlobalControl.shared.dayDetail = dayDetail
        GlobalControl.shared.monthSwitch = daySwitch
        pageTypeProtocol = children[0].view as! DUMonthDayView
        GlobalControl.shared.monthDay = pageTypeProtocol//children[0].view as! DUMonthDayView
        nowViewController = children[0]
        index = 0
        daySwitch.update()
        pageType = .DAY
        GlobalControl.shared.page = self
    }
    
    func showNowDate() {
        if children.count == 0 {
            return
        }
        if index == 0 || index == 1 {
            daySwitch.update()
            return
        }
        pageTypeProtocol = children[0].view as! DUMonthDayView
        GlobalControl.shared.monthDay = pageTypeProtocol//children[0].view as! DUMonthDayView
        daySwitch.update()
        transition(from: nowViewController!, to: children[0], options: .crossfade, completionHandler: nil)
        pageType = .DAY
        nowViewController = children[0]
        index = 0
    }
    
    private func showDate(year: Int, month: Int, option:NSViewController.TransitionOptions) {
        if month == 1 && year <= 1900 {
            return
        }
        if month == 12 && year >= 2199 {
            return
        }
        if children.count == 0 {
            return
        }
        var new:NSViewController
        if index == 1 || index == 2 || index == 3 {
            new = children[0]
            index = 0
        } else {
            new = children[1]
            index = 1
        }
        pageTypeProtocol = new.view as! DUMonthDayView
        GlobalControl.shared.monthDay = pageTypeProtocol
        transition(from: nowViewController!, to: new, options: option, completionHandler: nil)
        daySwitch.update(year: year, month: month)
        nowViewController = new
        pageType = .DAY
    }
    
    func preMonth(date: DateEntity) -> DateEntity {
        let d1 = CalendarUtil.monthDec(date: date)
        showDate(year: d1.solar.year, month: d1.solar.month, option: .slideRight)
        return d1
    }
    
    func prevPage() -> String {
        let v = nowViewController?.view as! DUSelectYearMonthView
        if v.page!.yearStart <= v.yearMin {
            return ""
        }
        let p = showDateSwitch(year: v.page!.yearStart - 1, month: v.page!.month)
        if p == nil {
            return ""
        }
        return p!.toString()
    }
    func nextPage() -> String {
        let v = nowViewController?.view as! DUSelectYearMonthView
        if v.page!.yearStart >= v.yearMax {
            return ""
        }
        let p = showDateSwitch(year: v.page!.yearEnd + 1, month: v.page!.month)
        if p == nil {
            return ""
        }
        return p!.toString()
    }
    
    func nextMonth(date: DateEntity) -> DateEntity {
        let d1 = CalendarUtil.monthInc(date: date)
        showDate(year: d1.solar.year, month: d1.solar.month, option: .slideLeft)
        return d1
    }
    
    func showYearMonth(year: Int, month:Int) -> String {
        var ret = ""
        if children.count == 0 {
            return ""
        }
        if pageType == PageSwitchEnum.MANY_YEAR {
            return ""
        }
        pageType = pageType.next()
        ret = showDateSwitch(year: year, month: month)?.toString() ?? ""
        return ret
    }
    
    func switchButtonHandler(page:PageTypeModel) {
        if page.pageType == PageSwitchEnum.MONTH {
            showDate(year: page.yearStart, month: page.month, option: .crossfade)
            return
        }
        var p = page
        p.pageType = p.pageType.pre()!
        daySwitch.monthButton.stringValue(p.toString())
        pageType = p.pageType//page.pageType.pre()
        let _ = showDateSwitch(year: p.yearStart, month: p.month)
    }
    
    private func showDateSwitch(year:Int, month:Int) -> PageTypeModel? {
        var p:PageTypeModel?
        var new:NSViewController
        if index == 0 || index == 1 || index == 2 {
            new = children[3]
            index = 3
        } else {
            new = children[2]
            index = 2
        }
        pageTypeProtocol = new.view as! DUSelectYearMonthView
        GlobalControl.shared.monthDay = pageTypeProtocol
        pageTypeProtocol.setPage(pageType: pageType, year: year, month: month, completionHandler: {(pageType:PageTypeModel?) in
            p = pageType
        });
        transition(from: nowViewController!, to: new, options: .crossfade, completionHandler: nil)
        nowViewController = new
        return p
    }
    
    func isSwitch() -> Bool {
        return index != 1 && index != 0 && index != -1
    }
}
